#!/usr/bin/env python

# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Logs information about method execution"""

import logging

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2011, Google Inc."
__license__ = "Apache License 2.0"
__version__ = "0.1"
__status__ = "Prototype"

__CRITICAL=50
__ERROR=40
__WARNING=30
__INFO=20
__DEBUG=10
__NOTSET=0

__LOG_LEVELS = {}

def __setup_loggable():
  __LOG_LEVELS['critical'] = 50
  __LOG_LEVELS['error'] = 40
  __LOG_LEVELS['warning'] = 30
  __LOG_LEVELS['info'] = 20
  __LOG_LEVELS['debug'] = 10
  __LOG_LEVELS['notset'] = 0        

def loggable(method = '', level = 'debug'):

  def inner_loggable(handler):
    
    def log_execution(self,*args):
      
      log_level = __LOG_LEVELS[level]
      if log_level:
        if method != '':
          logging.log(log_level, '--- starting executing method \'%s\' with name \'%s\' ---' % (handler.__name__,method))
        else:
          logging.log(log_level, '--- starting executing method \'%s\' ---' % handler.__name__)
        handler(self,*args)
        logging.log(log_level, '--- finishing method execution ---')        
      else:
        raise LevelError('required log level does not exist: %s' % level)
    
    return log_execution
  
  return inner_loggable

__setup_loggable()

class LevelError(Exception):
    """Raised when there is an error with getting the required level of logging."""
    def __init__(self, msg):
        Exception.__init__(self)
        self.msg = msg

    def __str__(self):
        return repr(self.msg) 

def setup():

  var_dict = globals()
  var_dict['log'] = loggable()


if __name__ == '__main__':
    pass    